/*  File Name	: HandleSet.h
 *  Author		: Javie Pong
 *	 E-mail		: 823960168@qq.com
 *  Created on	: 2012-3-20
 *  Description:
 */

#ifndef HANDLESET_H_
#define HANDLESET_H_
#include <sys/types.h>

class HandleSet
{
	friend class HandleSetIterator;
public:
	HandleSet();
	virtual ~HandleSet();

	void set_bit(int handle);

	void clr_bit(int handle);

	int is_set(int handle);

	fd_set *fdset (void);

	int max_handle();

	void set_max_handle(int handle);

	HandleSet & operator =(HandleSet &set);

	void reset();

private:
	fd_set mask_;
	int max_handle_;
	int min_handle_;
	int size_;
};

class HandleSetIterator
{
public:
	HandleSetIterator(HandleSet &hs);

	virtual ~HandleSetIterator();

	int operator () (void);

private:
	HandleSet handle_set_;
	fd_mask cur_mask_;
	int word_num_;
	int word_index_;
	int word_max_;
	int handle_index_;
};

class SelectHandleSet
{
public:
	SelectHandleSet();
	virtual ~SelectHandleSet();

	SelectHandleSet & operator =(SelectHandleSet &s);

	void set_max_handle(int max_handle);

	void set_bit(int handle);

	void clr_bit(int handle);

	void reset();

	HandleSet read_set_;
	HandleSet write_set_;
	HandleSet error_set_;
};

#endif /* HANDLESET_H_ */
